package com.redteam.xomtro;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.redteam.xomtro.entities.Room;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class RoomMapActivity extends MasterActivity implements GoogleMap.InfoWindowAdapter {
    private GoogleMap googleMap;
    private ArrayList<Room> roomArrayList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setActionBarMode(MasterActivity.ACTION_BAR_CHILD);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room_map);
        showActionBar();
        setTitle(getString(R.string.title_event_map));

        // Loading map
        initilizeMap();
        if (googleMap != null) {
            googleMap.setMyLocationEnabled(true);
            googleMap.getUiSettings().setMyLocationButtonEnabled(true);
            googleMap.getUiSettings().setCompassEnabled(true);
            googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                @Override
                public void onMapClick(LatLng latLng) {
                    showActionBar();
                }
            });
            googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                @Override
                public void onInfoWindowClick(Marker marker) {
                    if (marker != null) {
                        int i = Integer.valueOf(marker.getTitle());
                        Intent it = new Intent(RoomMapActivity.this, RoomDetailActivity.class);
                        it.putExtra(RoomDetailActivity.SELECTED_ROOM, roomArrayList.get(i));
                        startActivity(it);
                    }
                }
            });

            roomArrayList = Room.getListRoomExample();
            new BindingMap().execute(roomArrayList);

        }
    }

    @Override
    public View getInfoContents(Marker marker) {
        try {
            if (marker != null) {
                View v = View.inflate(RoomMapActivity.this, R.layout.marker_info_popup_details, null);
                final ImageView roomImg = (ImageView) v.findViewById(R.id.ivRoomImg);
                TextView roomTitle = (TextView) v.findViewById(R.id.tvRoomTitle);
                TextView roomAddress = (TextView) v.findViewById(R.id.tvRoomAddress);
//                TextView eventAttendees = (TextView) v.findViewById(R.id.tvRoomAttendees);
                TextView roomView = (TextView) v.findViewById(R.id.tvRoomView);

                int i = Integer.valueOf(marker.getTitle());
                roomTitle.setText(roomArrayList.get(i).getRoom_Name());
                roomTitle.setText(roomArrayList.get(i).getRoom_Address());
//                eventAttendees.setText(eventArrayList.get(i).getAttendeesCount() + "");
//                eventView.setText(roomArrayList.get(i).getViewCount() + "");
                roomImg.setImageResource(roomArrayList.get(i).getDemo_titleImage());
                roomImg.setScaleType(ImageView.ScaleType.CENTER_CROP);
//                imageLoader.displayImage(eventArrayList.get(i).getTitleImgUrl(), eventImg, options, new ImageLoadingListener() {
//                    @Override
//                    public void onLoadingStarted(String s, View view) {
//                    }
//
//                    @Override
//                    public void onLoadingFailed(String s, View view, FailReason failReason) {
//
//                    }
//
//                    @Override
//                    public void onLoadingComplete(String s, View view, Bitmap bitmap) {
//                        eventImg.setScaleType(ImageView.ScaleType.CENTER_CROP);
//                    }
//
//                    @Override
//                    public void onLoadingCancelled(String s, View view) {
//
//                    }
//                });
                return v;
            }
        } catch (Exception ignored) {
        }
        return null;
    }

    class BindingMap extends AsyncTask<ArrayList<Room>, Void, ArrayList<Room>> {

        @Override
        protected ArrayList<Room> doInBackground(ArrayList<Room>... arrayLists) {
            return arrayLists[0];
        }

        @Override
        protected void onPostExecute(ArrayList<Room> rList) {
            LatLng latLng;
            if (googleMap.getMyLocation() != null)
                latLng = new LatLng(googleMap.getMyLocation().getLatitude(), googleMap.getMyLocation().getLongitude());
            else
                latLng = new LatLng(rList.get(0).getRoom_Latitude(), rList.get(0).getRoom_Longitude());
            CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).zoom(12f).build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            for (int i = 0; i < rList.size(); i++) {
                googleMap.addMarker(new MarkerOptions().title(i + "")
                        .position(new LatLng(rList.get(i).getRoom_Latitude(), rList.get(i).getRoom_Longitude()))
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_marker_flooding)));
            }
            googleMap.setInfoWindowAdapter(RoomMapActivity.this);
//            super.onPostExecute(eList);
        }

    }

    private void initilizeMap() {
        if (googleMap == null) {
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.roomMapMarker);
            if (mapFragment != null)
                googleMap = mapFragment.getMap();
        }
    }

    public void showActionBar() {
        actionBar.show();
        new Timer("hide action bar").schedule(new TimerTask() {
            @Override
            public void run() {
                RoomMapActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        actionBar.hide();
                    }
                });
            }
        }, 3500);
    }

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }
}
